#!/bin/bash
source="\
kern/init/init.c \
kern/libs/stdio.c \
kern/debug/kdebug.c \
kern/debug/monitor.c \
kern/debug/panic.c \
kern/trap/trap.c \
kern/driver/picirq.c \
kern/driver/console.c \
kern/driver/intr.c \
kern/smp/mp.c \
kern/smp/lapic.c \
kern/smp/vm.c \
libs/printfmt.c \
libs/string.c \
"

rm -r bin obj bootother bootother.o bootother.out
mkdir bin obj obj/boot obj/sign obj/sign/tools obj/libs
include=`find kern/ libs/ -type d | grep -v "\.svn" | sed "s/^/obj\//"`
mkdir $include
include="-I"`echo $include | sed -e "s/ / -I/g" -e "s/-Iobj\//-I/g"`

gcc -fno-builtin -Wall -ggdb -m32 -nostdinc -fno-stack-protector -Os -nostdinc -c -Iboot/ -Ikern/mm/ kern/smp/bootother.S -o bootother.o
gcc -fno-builtin -Wall -ggdb -m32 -gstabs -nostdinc  -fno-stack-protector -Ikern/mm/ -c kern/init/entry.S -o obj/kern/init/entry.o
ld -m elf_i386 -N -e start -Ttext 0x7000 bootother.o -o bootother.out
objcopy -S -O binary bootother.out bootother

for i in $source
do
	gcc -fno-builtin -Wall -ggdb -m32 -nostdinc -fno-stack-protector $include -c $i -o obj/${i%.c}.o
done

ld -m elf_i386 -T tools/kernel.ld -o bin/kernel `find obj/kern/ obj/libs/ -name "*.o" | xargs` -b binary bootother

gcc -Iboot/ -fno-builtin -Wall -ggdb -m32 -nostdinc -fno-stack-protector -Ilibs/ -Os -nostdinc -c boot/bootasm.S -o obj/boot/bootasm.o
gcc -Iboot/ -fno-builtin -Wall -ggdb -m32 -nostdinc -fno-stack-protector -Ilibs/ -Os -nostdinc -c boot/bootmain.c -o obj/boot/bootmain.o
gcc -Itools/ -g -Wall -O2 -c tools/sign.c -o obj/sign/tools/sign.o
gcc -g -Wall -O2 obj/sign/tools/sign.o -o bin/sign
ld -m    elf_i386 -N -e start -Ttext 0x7C00 obj/boot/bootasm.o obj/boot/bootmain.o -o obj/bootblock.o
objcopy -S -O binary obj/bootblock.o obj/bootblock.out
bin/sign obj/bootblock.out bin/bootblock
dd if=/dev/zero of=bin/ucore.img count=10000
dd if=bin/bootblock of=bin/ucore.img conv=notrunc
dd if=bin/kernel of=bin/ucore.img seek=1 conv=notrunc
